package com.atguitu.campus.renwendemo.Controller;

import com.atguitu.campus.renwendemo.dto.AuthDto;
import com.atguitu.campus.renwendemo.dto.PageQueryBaseDto;
import com.atguitu.campus.renwendemo.entity.User;
import com.atguitu.campus.renwendemo.service.UserService;
import com.atguitu.campus.renwendemo.utils.AjaxResult;
import com.atguitu.campus.renwendemo.utils.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理", description = "用户相关的API接口")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/cli")
    public AjaxResult validMeth(@Valid @RequestBody AuthDto authDto) throws MyException {

        if ("12345".equals(authDto.getUsername())){
            throw new MyException("用户名太简单");
        }

        return AjaxResult.success("请求成功");
    }

    // 查询所有用户
    @Operation(summary = "获取所有用户", description = "返回所有用户的列表")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody User user) {
        //参数查询
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneEmpty(user.getName())) {
            queryWrapper.eq("name",user.getName());
        }

        return AjaxResult.success(userService.list(queryWrapper));
    }

    // 分页查询用户
    @PostMapping("/page")
    public AjaxResult page(@RequestBody PageQueryBaseDto dto) {
        Page<User> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNoneEmpty(dto.getKeyword())) {
            queryWrapper.like("name",dto.getKeyword());
        }
        return AjaxResult.success(userService.page(page,queryWrapper));
    }

    // 根据条件查询用户
    @PostMapping("/query")
    public AjaxResult query(@RequestParam String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return AjaxResult.success(userService.list(wrapper));
    }

    // 添加用户
    @PostMapping("/add")
    public AjaxResult add(@RequestBody User user) {
        boolean success = userService.save(user);
        if (success) {
            return AjaxResult.success(true); // 这里返回 true
        } else {
            return AjaxResult.error("添加失败");
        }

    }

    // 更新用户
    @PostMapping("/update")
    public AjaxResult update(@RequestBody User user) {
        boolean success = userService.updateById(user);
        if (success) {
            return AjaxResult.success(true); // 这里返回 true
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        return AjaxResult.success(userService.removeById(id));
    }
}
